package au.edu.unsw.infs3634.gamifiedlearning;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SearchView;

import com.google.android.material.navigation.NavigationView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.API.Country;
import au.edu.unsw.infs3634.gamifiedlearning.API.CountryService;
import au.edu.unsw.infs3634.gamifiedlearning.net.MyRetrofit;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Country_list extends AppCompatActivity{
    public static final String MESSAGE = "";
    private CountryAdapter mAdapter;
    private RecyclerView mRecyclerView;
    public EditText mEditText;
    public String savedText;
    String continentName;
    SharedPreferences sharedPref;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_country_list);
        mRecyclerView = findViewById(R.id.tvBookmarkList);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        //intent & set title
        Intent intent = getIntent();
        continentName = intent.getStringExtra(MESSAGE);
        setTitle(continentName);

        //get note
        mEditText = findViewById(R.id.etNotes);
        sharedPref = getSharedPreferences(savedText, MODE_PRIVATE);
        savedText = sharedPref.getString(continentName,"");
        Log.d("setText", savedText);
        if (savedText != null){
            mEditText.setText(savedText);
            Log.d(continentName, savedText);
        }

        CountryAdapter.RecyclerviewClickListener listener = new CountryAdapter.RecyclerviewClickListener() {
            @Override
            public void onClick(View view, String countryName) {
                launchInfo(countryName);
            }
        };
        mAdapter = new CountryAdapter(new ArrayList<Country>(), listener);

        ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
        CountryService service = MyRetrofit.getRetrofit().create(CountryService.class);
        Call<List<Country>> responseCall = service.getRegion(continentName);
        Log.d("Response Call", responseCall.toString());
        responseCall.enqueue(new Callback<List<Country>>() {
            @Override
            public void onResponse(Call<List<Country>> call, Response<List<Country>> response) {
                dialog.dismiss();
                List<Country> countries = response.body();
                Log.d("Country List", countries.toString());
                mAdapter.setData(countries);
                mRecyclerView.setAdapter(mAdapter);
                Log.d("Yes", (String.valueOf(mAdapter)));
            }

            @Override
            public void onFailure(Call<List<Country>> call, Throwable t) {
                dialog.dismiss();
                Log.d("No", String.valueOf(t));
            }
        });


        /** this is only for testing (useless now)
        TextView message = findViewById(R.id.tvChangeMain);
        Intent newIntent = getIntent();
        message.setText(newIntent.getStringExtra(MESSAGE));
        **/

    }
    //search & sort
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.country_list_menu, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                mAdapter.getFilter().filter(query);
                Log.d("Filter", query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                mAdapter.getFilter().filter(newText);
                Log.d("Filter", newText);
                return false;
            }
        });
        return true;
    }

    //Back button
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        finish();
        return true;
    }
    private void launchInfo(String message){
        Intent intentInfo = new Intent(Country_list.this, CountryInfo.class);
        intentInfo.putExtra(CountryInfo.countryName, message);
        startActivity(intentInfo);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.filterNameIncreasing:
                // sort by country name (A to Z)
                mAdapter.sort(1);
                return true;
            case R.id.filterNameDecreasing:
                // sort by country name (Z to A)
                mAdapter.sort(2);
                return true;
            case R.id.btnSubmit: {
                //save note
                savedText = mEditText.getText().toString();
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString(continentName,savedText);
                editor.commit();
                Log.d(continentName, savedText);
                Log.d("sharedpreference", sharedPref.getString(continentName,""));
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}